﻿using System;
using System.Collections.Generic;
using UnityEngine;
public class AssetCaching
{
    /// <summary>
    /// 缓存已经加载号的资源
    /// </summary>
    private Dictionary<string, TempObject> cacheList = null;

    public AssetCaching()
    {
        cacheList = new Dictionary<string, TempObject>();
    }
    
    public void AddAsset(string assetName,TempObject obj)
    {
        if (!cacheList.ContainsKey(assetName))
        {
            cacheList.Add(assetName, obj);
        }
        else
        {
            Debug.LogWarning("资源"+assetName+"重复添加");
        }
    }
    public UnityEngine.Object[] GetAssets(string assetName)
    {
        if (cacheList.ContainsKey(assetName)) return cacheList[assetName].assets.ToArray();
        Debug.LogWarning("此资源" + assetName + "还没加载完成");
        return null;
    }
    public void UnLoadAsset(string assetName)
    {
        if (cacheList.ContainsKey(assetName))
        {
            cacheList[assetName].UnLoadAsset();
            return;
        }
        Debug.LogWarning("此资源" + assetName + "还没加载完成");
    }
    public void UnLoadAllAsset()
    {
        foreach(var asset in cacheList)
        {
            asset.Value.UnLoadAsset();
        }
        cacheList.Clear();
    }
}